package xin.marcher.wind.process.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import xin.marcher.framework.mybatis.dao.BaseDAO;
import xin.marcher.wind.process.domain.entity.ProcessNodeDO;
import xin.marcher.wind.process.domain.request.ProcessNodeQuery;
import xin.marcher.wind.process.mapper.ProcessNodeMapper;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 流程节点表 DAO
 * </p>
 *
 * 
 */
@Repository
public class ProcessNodeDAO extends BaseDAO<ProcessNodeMapper, ProcessNodeDO> {

    /**
     * 根据name查询流程节点
     * @param name
     * @return
     */
    public ProcessNodeDO getOne(String name) {
        if(StringUtils.isEmpty(name)) {
            return null;
        }
        LambdaQueryWrapper<ProcessNodeDO> query = new LambdaQueryWrapper<>();
        query.eq(ProcessNodeDO::getName, name);
        List<ProcessNodeDO> list = list(query);
        if(CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    public void deleteByName(String name) {
        if(StringUtils.isEmpty(name)) {
            return;
        }
        LambdaQueryWrapper<ProcessNodeDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProcessNodeDO::getName,name);

        remove(queryWrapper);
    }

    public List<ProcessNodeDO> listBy(ProcessNodeQuery query) {
        LambdaQueryWrapper<ProcessNodeDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!Objects.isNull(query.getType()),ProcessNodeDO::getType,query.getType())
                .like(StringUtils.isNotEmpty(query.getName()),ProcessNodeDO::getName,query.getName());
        return list(queryWrapper);
    }

}
